package min3d.sampleProject1;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import min3d.core.Renderer;
import min3d.core.Scene;
import min3d.objectPrimitives.Sphere;
import min3d.vos.Color4;
import android.content.Context;
import android.opengl.GLU;
import android.util.Log;

public class MyRenderer extends Renderer{
	TexFont tf;
	Scene scene;
	Context context;
	MyActivity ra;
	int textXoffset = 20;
	int textYtop = 750;
	int textYoffset = 50;
	int textYcounter = 0;
	Color4 red = new Color4(255, 0, 0, 255);
	Color4 blue = new Color4(0, 0, 255, 255);

	/**
	 * Constructor
	 * @param MyActivity 	A handle to the activity object.
	 */
	public MyRenderer(MyActivity ra){
		super(ra.scene);
		scene = ra.scene;
		context = ra.getApplicationContext();
		this.ra = ra;
	}
	
	/**
	 * Calls super's onSurfaceCreated and setupTexFont.
	 * @param  GL10 			GL10 context.
	 * @param  EGLConfig 		EGLConfig configuration object.
	 * @return void
	 */
	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig eglConfig) {
		super.onSurfaceCreated(gl, eglConfig);
		setupTexFont(gl);
	}
	
	/**
	 * Loads a font from the assets folder and initializes TexFont.
	 * @param  GL10 			GL10 context.
	 * @return void
	 */
	private void setupTexFont(GL10 gl){
		tf = new TexFont(context, gl);
		try{
			if(tf.LoadFont("font.bff", gl))
				Log.v("", "Font loaded");
			else
				Log.v("", "Couldn't load font");
		}catch(Exception e){
			Log.v("", "Exception while loading font");
		}
	}
		
	/**
	 * Calculates the offset of the next line of text.
	 * @return int  	The offset. 
	 */
	private int getYoffset(){
		return textYtop-textYoffset*textYcounter++;
	}
	
	/**
	 * Draws a sphere for the indicated player at their position.
	 * @param  int 			Number indicating the player.
	 * @return void
	 */
	private void drawSphere(int player){
    	// check size
    	if(ra.spheres.size() > ra.spheresThreshold)
    		ra.scene.removeChild(ra.spheres.remove());
    	// insert sphere
        Sphere s;
        if(player == 1){
        	s = new Sphere(10, 4, 4, blue);
        	s.position().x = ra.posX;
        	s.position().y = ra.posY;
        	s.position().z = ra.posZ;
		}
        else{
        	s = new Sphere(10, 4, 4, red);
        	s.position().x = ra.headX;
        	s.position().y = ra.headY;
        	s.position().z = ra.headZ;
        }
        s.vertexColorsEnabled(false);
        s.colorMaterialEnabled(true);
        s.lightingEnabled(true);
        
        ra.spheres.add(s);
        ra.scene.addChild(s);
	}
	
	/**
	 * Android OpenGL ES Hook that is called each time a frame is drawn.
	 * Calls super class and then handles handles drawing, info text and lookAt.
	 * @param  GL10 			GL10 context.
	 * @return void
	 */
	@SuppressWarnings("static-access")
	@Override
	public void onDrawFrame(GL10 gl){
		// draw scene graph
		super.onDrawFrame(gl);
		// draw spheres
		if(!ra.myMenu.interactive && !ra.spheres.isEmpty()){
			for(Sphere s : ra.spheres)
				ra.scene.removeChild(s);
			ra.spheres.clear();
		}
		
		//Log.v("LAWL", "RA: " + ra.trackerLocation);
		if(ra.trackerLocation != null){
			if(ra.myMenu.interactive && ra.trackerLocation.contains("1draw"))
				drawSphere(1);
			if(ra.myMenu.interactive && ra.trackerLocation.contains("2draw"))
				drawSphere(2);
		}
		// draw info text
		if(ra.myMenu.showInfo){
			textYcounter = 0; // increments on getYoffset() calls
			gl.glDisable(GL10.GL_DEPTH_TEST); // needed to show all chars

			tf.PrintAt(gl, "FPS: " + ra.fps, textXoffset, getYoffset());
			tf.PrintAt(gl, "VERTICES: " + ra.vertices, textXoffset, getYoffset());
			tf.PrintAt(gl, "FACES: " + ra.faces, textXoffset, getYoffset());
			if(ra.myMenu.showModel)
				tf.PrintAt(gl, "SHOWING MODEL ", textXoffset, getYoffset());
			if(ra.myMenu.trackingOn)
				tf.PrintAt(gl, "TRACKING ", textXoffset, getYoffset());
			if(ra.myMenu.interactive)
				tf.PrintAt(gl, "INTERACTIVE ", textXoffset, getYoffset());

			gl.glEnable(GL10.GL_DEPTH_TEST); // turn back on
		}
		// update lookAt
		GLU.gluLookAt(gl,
				0, 0, 1000,
				0, 0, 1000,
				ra.scene.camera().upAxis.x,
				ra.scene.camera().upAxis.y,
				ra.scene.camera().upAxis.z);
	}
}
